<?php include('../../include/functions.inc.php'); ?>
<link href="../../css/ContentStyle.css" rel="stylesheet" type="text/css" />
<link href="../../css/tableorderer.css" rel="stylesheet" type="text/css" />
<div id="datacontainer">
    <table width="100%" cellpadding="0" cellspacing="1" class="table table-striped table-hover table-bordered">
        <tbody>
            <?php
            $strQuery = "SELECT  cllg_account.ID FK_ACCOUNT,
                                cllg_cheque.NUMERO 
                        FROM    cllg_cheque,
                                cllg_factura,
                                cllg_account
                        WHERE   cllg_account.ID = " . getParam("ACCOUNT") . "
                        AND     cllg_cheque.STATUS = 0
                        AND     cllg_cheque.FECHA_REPOSICION IS NULL
                        AND     cllg_cheque.FK_FACURA = cllg_factura.ID
                        AND     cllg_factura.FK_STUDENT = cllg_account.FK_STUDENT";
            $arrInfo = $db->query_toArray($strQuery);
            if (isset($arrInfo[0])) {
                ?>
                <tr>
                    <td colspan="8" style="color: red; font-size: 15px;">El estudiante tiene cheque(s) rechazado(s)!!!</td>
                </tr>    
                <?php
            }
            ?>
            <tr>
                <th width="5%" style="width:5%" >&nbsp;</th>
                <th width="20%" >Cargo</th>
                <th width="13%" >Fecha Pago</th>
                <th width="9%" >Abonos</th>
                <th width="18%" >Saldo Pendiente</th>
                <th width="8%" >Mora</th>
                <th width="15%" >Total a Pagar</th>
                <th width="25%" >Este Abono</th>
            </tr>
            <?php
            $f = date('y-d-m');
            $r = $db->query_select("cllg_cargo", "(DATE_PAYED IS NULL OR TOTAL_PENDIENTE>0 ) AND FK_ACCOUNT=" . getParam("ACCOUNT"), "DATE_LIMIT");
            $index = 0;
            $co = 0;
            foreach ($r as $c) {
                $sig = $c["MONEDA"] == 1 ? "Q" : "$";
                $now = date("Y-m-d", strtotime(getParam("FECHAPAGO")));
                //echo $now;
                $penal = 0;
                $total = $c['AMOUNT'];
                $originalTotal = $total;
                $parciales = "No";

                if (isset($c["TOTAL_PENDIENTE"]) && $c["TOTAL_PENDIENTE"] > 0) {
                    $total = $c['TOTAL_PENDIENTE'];
                    $originalTotal = $total;
                    $parciales = "Si";
                }
                if ($c["TOTAL"] > 0) { // SI NO TIENE PAGOS ANTERIORES
                    if ($c["TOTAL"] > $c['AMOUNT']) { //SI NO TIENE DESCUENTOS
                        $montoMora = $c['PENALTY'] + $c['AMOUNT'];
                    } else {
                        $sum = getTotalAbono($db, $c["ID"]);
                        $montoMora = $sum + $c['TOTAL_PENDIENTE'];
                    }
                    if ($montoMora != $c['TOTAL']) { //SI NO TIENE MORA APLICADA
                        if (strtotime($now) > strtotime($c['DATE_LIMIT'])) {
                            $penal = $c['PENALTY'];
                            $total+=$penal;
                        }
                    }
                } else {
                    if (strtotime($now) > strtotime($c['DATE_LIMIT'])) {
                        $penal = $c['PENALTY'];
                        $total+=$penal;
                    }
                }
                
                if($c["TOTAL_PENDIENTE"] > 0 && strtotime($now) > strtotime($c['DATE_LIMIT'])){
                    $cargo = $c['AMOUNT'] + $c['PENALTY'];
                    if($c['TOTAL'] < $cargo){
                        $penal = $c['PENALTY'] != null ? $c['PENALTY'] : 0;
                        $total += $penal;
                    }
                }

                $row = array("ID" => $c["ID"], "MONEDA" => $c["MONEDA"],
                    "DESCRIPTION" => $c['DESCRIPTION'],
                    "AMOUNT" => number_format($c['AMOUNT'], 2, ".", ","),
                    "PENAL" => number_format(floatval($penal), 2, ".", ","),
                    "PENAL_REF" => number_format(floatval($penal), 2, ".", ","),
                    "TOT" => $total);
                $index = $c["ID"];
                $json_string = json_encode($row);
                ?>
                <tr class="<?php echo fmod($co, 2) == 0 ? 'data line0' : 'data line1'; ?>" >
                    <td><input type="checkbox" id="chk_<?php echo $index; ?>" name="chk_<?php echo $index; ?>" onclick='javascript:doit(this,<?php echo $json_string; ?>);' /></td>
                    <td><?php echo $c['DESCRIPTION']; ?></td>
                    <td><?php echo $c['DATE_LIMIT']; ?></td>
                    <td><?php echo $parciales; ?></td>
                    <td><?php echo $sig . " " . number_format($originalTotal, 2, ".", ","); ?></td>
                    <td><?php echo $sig . " " . number_format(floatval($penal), 2, ".", ","); ?></td>
                    <td><?php echo $sig . " " . number_format($total, 2, ".", ","); ?></td>
                    <td>
                        <input type="text" id="txt_<?php echo $index; ?>" readonly="readonly" name="txt_<?php echo $index; ?>" value="<?php echo number_format($total, 2, ".", ","); ?>" onkeyup='javascript:setAbono(this,<?php echo $row["ID"]; ?>);' />
                    </td>
                </tr>
                <?php
                $co++;
            }
            ?>
        </tbody>
    </table>
</div>